package com.ruoyi.web.controller.baodao.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.baodao.entity.EvtRecordSale;
import com.ruoyi.baodao.entity.PrintReceiptDetail;
import com.ruoyi.baodao.entity.ZSReview;
import com.ruoyi.baodao.mapper.BDMapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 行为复核
 */
@Slf4j
@Controller
@RequestMapping("baodao/reviewxw")
public class ReviewXWController extends BaseController {
    @Autowired
    BDMapper bdMapper;
    private String prefix = "baodao/reviewxw";

    @GetMapping()
    public String operlog()
    {
        return prefix + "/reviewxw";
    }

    // 查询 追溯小票复核数据
    /**
     * 药品主档
     */
    @PostMapping("/list")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo list(String sellStartTime,String sellEndTime, String flag)

    {

        logger.info("查询追溯监管复核，时间参数：" + sellStartTime+" and "+ sellEndTime);
        // 获取 用户名 和  用户 部门
        // List<MFGoodsLink> mfGoodsLinks = mfGoodsLinkMapper.selectByExample(new MFGoodsLinkExample());
        // 判断无时间参数返回空
        if(StringUtils.isEmpty(sellStartTime) && StringUtils.isEmpty(sellEndTime)) {
            List<ZSReview> emptyList = new ArrayList<ZSReview>();
            return getDataTable(emptyList);
        }

        // 取部门编号（门店号）
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();

        startPage(); //分页

        List<ZSReview> zsReviewList =  bdMapper.queryZSReview(shopId,sellStartTime,sellEndTime);

       return getDataTable(zsReviewList);

    }


    /**
     * 查看详细信息
     */
    @GetMapping("/detail/{sellNo}")
    public String detail(@PathVariable("sellNo") String sellNo,String startTime,String endTime, ModelMap modelMap)
    {

        String recordUrl = getRecordUrl(startTime);

        // 上传文件路径
        String apiUrl = RuoYiConfig.getApiurl();
        if("16396336".equals(sellNo)
                || "16396358".equals(sellNo)
                || "16396370".equals(sellNo)
                || "16396408".equals(sellNo)
                || "16396418".equals(sellNo)
                || "16396442".equals(sellNo)
        ){
            recordUrl = apiUrl + "/profile/ipc/"+sellNo+".ts";// 测试用
        }



        modelMap.put("key01","123");
        modelMap.put("sellNo",sellNo);
        modelMap.put("startTime",startTime);
        modelMap.put("endTime",endTime);

        modelMap.put("faceImgUrl",apiUrl + "/profile/face/"+sellNo+".jpg");
        //modelMap.put("flvUrl","http://10.0.1.116:30003/profile/ipc/"+sellNo+".mp4");
        //modelMap.put("flvUrl","http://10.0.1.116:30003/profile/ipc/1613961321241120.ts");
        //modelMap.put("flvUrl",apiUrl + "/profile/ipc/"+sellNo+".ts");   // 测试用
        modelMap.put("flvUrl",recordUrl);     //
        logger.info("票号：" + sellNo);

        return prefix + "/detail";
    }


    public String getRecordUrl(String happened_time){
        // 上传文件路径
        String apiUrl = RuoYiConfig.getApiurl();
        //apiUrl = "http://10.0.1.116:30003";

        try {

            // 获取回放视频片段url
            Map<String, String> params = new HashMap<>();
            params.put("happened_time", happened_time);
            String result = HttpUtils.doPost(apiUrl + "/api_sunmi/getRecordUrl.aspx", params);

            JSONObject ret = JSON.parseObject(result);
            if(ret.getInteger("code").equals(0)){
                String recordUrl = ret.getString("recordUrl");
                return recordUrl;
            }
        } catch (Exception ex) {

        }
        return "";

    }



    /**
     * 小票数据
     */
    @PostMapping("/list/receipt")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo receiptlist(String sellNo,String startTime, String endTime)

    {

        logger.info("查询小票打印数据，参数：" + sellNo );
        // 获取 用户名 和  用户 部门
        // List<MFGoodsLink> mfGoodsLinks = mfGoodsLinkMapper.selectByExample(new MFGoodsLinkExample());


        // 取部门编号（门店号）
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();

        startPage(); //分页
        List<PrintReceiptDetail> printReceiptDetailList =  bdMapper.selectPrintReceiptDetailList(shopId,sellNo);

        return getDataTable(printReceiptDetailList);

    }

    /**
     * 药品主档
     */
    @PostMapping("/list/zsRecord")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public TableDataInfo zsRecordlist(String sellNo,String startTime, String endTime)

    {

        logger.info("查询追溯监管复核，时间参数：" + startTime+" and "+ endTime);
        // 获取 用户名 和  用户 部门
        // List<MFGoodsLink> mfGoodsLinks = mfGoodsLinkMapper.selectByExample(new MFGoodsLinkExample());


        // 取部门编号（门店号）
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysUser.getDept();
        String shopId = sysUser.getDept().getDeptName();

        startPage(); //分页
        List<EvtRecordSale> evtRecordSaleList =  bdMapper.selectEvtRecordSaleList(shopId,startTime,endTime);

        return getDataTable(evtRecordSaleList);

    }

}
